package com.en.controller;

import com.alibaba.fastjson2.JSON;
import com.en.common.Utils;
import com.en.model.Message;
import com.en.model.Word;
import com.en.response.ErrorCodeMsgEnum;
import com.en.response.R;
import com.en.service.WordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * ClassName: wordController
 * Package: com.tencent.wxcloudrun.controller
 * Description:
 *
 * @Date: 2023/11/19 08:07
 * @Author: wht
 */
@Slf4j
@RestController
@RequestMapping("/word")
public class WordController {
    @Autowired
    private WordService wordService;

    @GetMapping("/get_all_word")
    public <T> R<T> getAllWord(@RequestParam(required = true, value = "word_version") Integer word_version) {
        List<Word> wordList = wordService.getAllWord(word_version);
//        HashMap<Integer, Object> wordMap = new HashMap<>();
//        for (Word word : wordList) {
//            wordMap.put(word.getId(), word);
//        }
        HashMap<String, String> res = new HashMap<>();
        res.put("word_version", "1");
        res.put("value", JSON.toJSONString(wordList));

        return R.successData(res);
    }


    @GetMapping("/get_one_word")
    public <T> R<T> getOneWord(@RequestParam(required = false, value = "word") String word, @RequestParam(required = false, value = "id") Integer id) {

        if (word != null && !word.isEmpty()) {
            Word wordObj = wordService.getOneWordByWord(word);
            return R.successData(wordObj);
        }

        if (id != null) {
            Word wordObj = wordService.getById(id);
            return R.successData(wordObj);
        }

        return R.error(ErrorCodeMsgEnum.SERVICE_ERROR);

    }

//    @PostMapping("/verifyTranslate")
//    public <T> R<T> verifyTranslate() {
//
//        HashMap<String, String> headers = new HashMap<>();
//        headers.put("Authorization", "Bearer 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");
//
//
//        ArrayList<Message> messageList = new ArrayList<>();
//        messageList.add(new Message("System", "You are a helpful assistant. Answer as concisely as possible."));
//        messageList.add(new Message("user", "hello"));
//
//
//        HashMap<String, Object> body = new HashMap<>();
//        body.put("model", "gpt-3.5-turbo");
//        body.put("max_tokens", 1000);
//        body.put("temperature", 1);
//        body.put("top_p", 1);
//        body.put("messages", messageList);
//        body.put("stream", false);
//        body.put("user", "user_id");
//
//        log.info("333333333333");
//        String s = Utils.httpPost("https://api.openai.com/v1/chat/completions", headers, body);
//        log.info(s);
//
//
//        return R.error(ErrorCodeMsgEnum.SERVICE_ERROR);
//
//    }


}
